package testDemo.Test4;

import javax.print.DocFlavor;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

class Student{
    private String name;
    private int chinese;
    private int math;
    private int english;

    public Student() {
        super();
    }

    public Student(String name, int chinese, int math, int english) {
        super();
        this.name = name;
        this.chinese = chinese;
        this.math = math;
        this.english = english;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getChinese() {
        return chinese;
    }

    public void setChinese(int chinese) {
        this.chinese = chinese;
    }

    public int getMath() {
        return math;
    }

    public void setMath(int math) {
        this.math = math;
    }

    public int getEnglish() {
        return english;
    }

    public void setEnglish(int english) {
        this.english = english;
    }
    public int getSum(){
        return this.chinese+this.math+this.english;
    }
}
public class Main {
    public static void main(String[] args) throws IOException {
        TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                int num = s2.getSum() - s1.getSum();
                int num2= num == 0 ? s1.getChinese() - s2.getChinese() : num;
                int num3= num2 == 0 ? s1.getMath() - s2.getMath() : num2;
                int num4 = num3 == 0 ? s1.getName().compareTo(s2.getName()):num3;
                return num4;
            }
        });
        for (int i = 0; i < 1; i++) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入第"+(i+1)+"个学生信息：");
            System.out.println("姓名：");
            String name = sc.next();
            System.out.println("语文成绩:");
            int chinese = sc.nextInt();
            System.out.println("数学成绩:");
            int math = sc.nextInt();
            System.out.println("英语成绩:");
            int english = sc.nextInt();

            //创建学生对象，把键盘录入的数据赋值给学生对象或者成员变量
            Student s=new Student();
            s.setName(name);
            s.setChinese(chinese);
            s.setMath(math);
            s.setEnglish(english);

            ts.add(s);
        }
        //创建字符缓冲输出对象 BufferedWriter
        BufferedWriter bw = new BufferedWriter(new FileWriter(Main.class.getResource("/a.txt").getFile()));
        for (Student s:ts){
            StringBuilder sb = new StringBuilder();
            sb.append(s.getName()).append(",").append(s.getChinese()).append(",").append(s.getMath()).append(",").append(s.getEnglish()).append(",").append(s.getSum());

            //调用输出流写数据
            bw.write(sb.toString());
            bw.newLine();
            bw.flush();
        }
        bw.close();
    }
}